import { Response } from '@angular/http';
import { Headers, } from '@angular/http';
import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { Interceptor, InterceptedRequest, InterceptedResponse } from 'ng2-interceptors';
declare var $: any;
declare var bootbox;

@Injectable()
export class ConfigInterceptor implements Interceptor {
    constructor(private Router: Router) { }
    public interceptBefore(request: InterceptedRequest): InterceptedRequest {
        request.options.withCredentials = true;
        return request;
    }

    public interceptAfter(response: InterceptedResponse): InterceptedResponse {
        let Response = response.response.json();
        if (!Response.success) {
            if (Response.errorCode === "1003") {
                $.alert("验证码错误！")
                return;
            } else if (Response.errorCode === "40001") {
                $.alert("订单不存在！")
                return;
            } else if (Response.errorCode === "10006") {
                $.alert("参数不能为空！")
                return;
            } else if (Response.errorCode === "10003") {
                $.alert("未能发送短信到手机！")
                return;
            } else if (Response.errorCode === "10004") {
                $.alert("没有访问权限！")
                return;
            } else if (Response.errorCode === "10000") {
                $.alert("错误编码定义错误！")
                return;
            }else if (Response.errorCode === "-1") {
                $.alert("活动编号或参数不正确！")
                return;
            }else if (Response.errorCode === "70002") {
                $.alert("活动不存在！")
                return;
            }else if (Response.errorCode === "0") {
                $.alert("接口校验失败！")
                return;
            }
            //标准化
            response.response["_body"] = response.response.json().data;
            return response;
            // throw response;
        }
        //标准化
        response.response["_body"] = response.response.json().data;
        return response;
    }
}
